# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
from pymongo import MongoClient

"""
WebDriver 只能在一个页面上对元素识别与定位，对于frame/iframe 表单内嵌页面上的元素无法直接定位
需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe 表单的内嵌页面中
#切换到表单
driver.switch_to.frame("x-URS-iframe")
打开新的标签页
js=f'window.open("{url}");'
driver.execute_script(js)
------Select------
# 选中这个标签，然后使用Select创建对象
selectTag = Select(driver.find_element_by_name("jumpMenu"))
# 根据索引选择
selectTag.select_by_index(1)
# 根据值选择
selectTag.select_by_value("值")
# 根据可视的文本选择
selectTag.select_by_visible_text("显示值")
# 取消选中所有选项
selectTag.deselect_all()
------Select------
"""

# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待
    wait = WebDriverWait(driver, 10)
    driver.find_element_by_xpath('//button[@class="check"]').click()
    driver.find_element_by_xpath('//input[@id="weixinpass_num"]').send_keys("18061")
    driver.find_element_by_xpath('//button[@id="weixindown"]').click()

    # WebDriverWait(driver, 10).until(
    #     EC.presence_of_element_located((By.ID, "q"))).send_keys("金融市场")
    # driver.find_element_by_css_selector('[class="searchSub search_btn"]').click()
    # # 点击下一页
    # driver.find_element_by_xpath('//li[@class="next"]/a').click()

    selector = etree.HTML(driver.page_source)
    xpath = selector.xpath('//div[@class="main_choice"]')
    for i in xpath:
        dict_data = dict()
        num = 0
        dict_data['title'] = "".join(i.xpath('./p/a//text()'))
        for c in i.xpath('./text()'):
            tmp = c.replace("\r\n", "").strip()
            if (tmp != ""):
                num = num + 1
                dict_data['content_' + str(num)] = tmp
        dict_data['answer'] = "".join(i.xpath('./p[2]//text()'))
        insertItem("aa", dict_data)

def insertItem(tableName, data):
    my_set = db[tableName]
    my_set.insert_one(data)




def close_driver(driver):
    driver.quit()

if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    url = "https://www.ehafo.com/paper-127994"
    driver = init_driver(url)
    get_url(driver)
